package gui.Paneli;

import javax.swing.JPanel;

import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Sportisti extends JPanel {
	
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JButton btnDodajpromeni;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	
	public String Ime;
	public String Prezime;
	public String DatumRodjenja;
	public String JMBG;
	public String GodinaPristupaKlubu;
	public String Adresa;
	public String Telefon;
	public String Pojas;

	/**
	 * Create the panel.
	 */
    public  Sportisti(){
        
    	setPreferredSize(new Dimension(800,450));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{100, 200, 100, 200, 0, 0, 0, 175, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lbIme = new JLabel("Ime");
		lbIme.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblIme = new GridBagConstraints();
		gbc_lblIme.anchor = GridBagConstraints.WEST;
		gbc_lblIme.insets = new Insets(10, 10, 5, 5);
		gbc_lblIme.gridx = 0;
		gbc_lblIme.gridy = 0;		
		add(lbIme, gbc_lblIme);
		
		textField_2 = new JTextField();
		GridBagConstraints gbc_textField_2 = new GridBagConstraints();
		gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_2.insets = new Insets(10, 0, 5, 5);
		gbc_textField_2.gridx = 1;
		gbc_textField_2.gridy = 0;
		add(textField_2, gbc_textField_2);
		textField_2.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Prezime");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel.insets = new Insets(10, 5, 5, 5);
		gbc_lblNewLabel.gridx = 2;
		gbc_lblNewLabel.gridy = 0;
		add(lblNewLabel, gbc_lblNewLabel);
		
		textField_3 = new JTextField();
		GridBagConstraints gbc_textField_3 = new GridBagConstraints();
		gbc_textField_3.insets = new Insets(10, 0, 5, 5);
		gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_3.gridx = 3;
		gbc_textField_3.gridy = 0;
		add(textField_3, gbc_textField_3);
		textField_3.setColumns(10);
		
		JLabel lblDatumRodjenja = new JLabel("Datum rodjenja");
		lblDatumRodjenja.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblDatumRodjenja = new GridBagConstraints();
		gbc_lblDatumRodjenja.anchor = GridBagConstraints.WEST;
		gbc_lblDatumRodjenja.insets = new Insets(5, 10, 5, 5);
		gbc_lblDatumRodjenja.gridx = 0;
		gbc_lblDatumRodjenja.gridy = 1;
		add(lblDatumRodjenja, gbc_lblDatumRodjenja);
		
		textField_4 = new JTextField();
		GridBagConstraints gbc_textField_4 = new GridBagConstraints();
		gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_4.weightx = 0.5;
		gbc_textField_4.insets = new Insets(5, 0, 5, 5);
		gbc_textField_4.gridx = 1;
		gbc_textField_4.gridy = 1;
		add(textField_4, gbc_textField_4);
		textField_4.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("JMBG");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel_1.insets = new Insets(5, 5, 5, 5);
		gbc_lblNewLabel_1.gridx = 2;
		gbc_lblNewLabel_1.gridy = 1;
		add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		textField_5 = new JTextField();
		GridBagConstraints gbc_textField_5 = new GridBagConstraints();
		gbc_textField_5.insets = new Insets(5, 0, 5, 5);
		gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_5.gridx = 3;
		gbc_textField_5.gridy = 1;
		add(textField_5, gbc_textField_5);
		textField_5.setColumns(10);
		
		JLabel lblGodinaPristupaKlubu = new JLabel("Godina pristupa klubu");
		lblGodinaPristupaKlubu.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblGodinaPristupaKlubu = new GridBagConstraints();
		gbc_lblGodinaPristupaKlubu.anchor = GridBagConstraints.WEST;
		gbc_lblGodinaPristupaKlubu.insets = new Insets(5, 10, 5, 5);
		gbc_lblGodinaPristupaKlubu.gridx = 0;
		gbc_lblGodinaPristupaKlubu.gridy = 2;
		add(lblGodinaPristupaKlubu, gbc_lblGodinaPristupaKlubu);
		
		textField_6 = new JTextField();
		GridBagConstraints gbc_textField_6 = new GridBagConstraints();
		gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_6.insets = new Insets(5, 0, 5, 5);
		gbc_textField_6.gridx = 1;
		gbc_textField_6.gridy = 2;
		add(textField_6, gbc_textField_6);
		textField_6.setColumns(10);
		
		JLabel lblAdresa = new JLabel("Adresa");
		lblAdresa.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblAdresa = new GridBagConstraints();
		gbc_lblAdresa.anchor = GridBagConstraints.WEST;
		gbc_lblAdresa.insets = new Insets(5, 5, 5, 5);
		gbc_lblAdresa.gridx = 2;
		gbc_lblAdresa.gridy = 2;
		add(lblAdresa, gbc_lblAdresa);
		
		textField_7 = new JTextField();
		GridBagConstraints gbc_textField_7 = new GridBagConstraints();
		gbc_textField_7.insets = new Insets(5, 0, 5, 5);
		gbc_textField_7.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_7.gridx = 3;
		gbc_textField_7.gridy = 2;
		add(textField_7, gbc_textField_7);
		textField_7.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Kontakt telefon");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel_2.insets = new Insets(5, 10, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 3;
		add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		textField_8 = new JTextField();
		GridBagConstraints gbc_textField_8 = new GridBagConstraints();
		gbc_textField_8.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_8.insets = new Insets(5, 0, 5, 5);
		gbc_textField_8.gridx = 1;
		gbc_textField_8.gridy = 3;
		add(textField_8, gbc_textField_8);
		textField_8.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("Trenutni pojas [KYU]");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel_3.insets = new Insets(5, 5, 5, 5);
		gbc_lblNewLabel_3.gridx = 2;
		gbc_lblNewLabel_3.gridy = 3;
		add(lblNewLabel_3, gbc_lblNewLabel_3);
		
		textField_9 = new JTextField();
		GridBagConstraints gbc_textField_9 = new GridBagConstraints();
		gbc_textField_9.insets = new Insets(5, 0, 5, 5);
		gbc_textField_9.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_9.gridx = 3;
		gbc_textField_9.gridy = 3;
		add(textField_9, gbc_textField_9);
		textField_9.setColumns(10);
		
		btnDodajpromeni = new JButton("Dodaj/Promeni");
		btnDodajpromeni.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//kod za upis u xml
				Ime = textField_2.getText();
				Prezime = textField_3.getText();
				DatumRodjenja = textField_4.getText();
				JMBG = textField_5.getText();
				GodinaPristupaKlubu = textField_6.getText();
				Adresa = textField_7.getText();
				Telefon = textField_8.getText();
				Pojas = textField_9.getText();
				
				Document doc;
				try  
		        {  
		            doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();  
		        }  
		        catch ( ParserConfigurationException ex )  
		        {  
		            // TODO Auto-generated catch block  
		            ex.printStackTrace();  
		            return;  
		        }  
				// Create root element and add to document  
		        Element root = doc.createElement( "root" );  
		        doc.appendChild( root );  
		        
		        // Create 8 child elements (for each textfield) 
		        Element child1 = doc.createElement( "ime" );  
		        Element child2 = doc.createElement( "prezime" ); 
		        Element child3 = doc.createElement( "datum" );  
		        Element child4 = doc.createElement( "jmbg" ); 
		        Element child5 = doc.createElement( "godinaPristupa" );  
		        Element child6 = doc.createElement( "adresa" ); 
		        Element child7 = doc.createElement( "telefon" );  
		        Element child8 = doc.createElement( "pojas" ); 
		        
		     // Set attribute 'value' for child elements  
		        child1.setAttribute( "value", Ime );  
		        child2.setAttribute( "value", Prezime );  
		        child3.setAttribute( "value", DatumRodjenja );  
		        child4.setAttribute( "value", JMBG );  
		        child5.setAttribute( "value", GodinaPristupaKlubu );  
		        child6.setAttribute( "value", Adresa );  
		        child7.setAttribute( "value", Telefon );  
		        child8.setAttribute( "value", Pojas );  
		        
		     // Add the child elements to root  
		        root.appendChild( child1 );  
		        root.appendChild( child2 ); 
		        root.appendChild( child3 );  
		        root.appendChild( child4 ); 
		        root.appendChild( child5 );  
		        root.appendChild( child6 ); 
		        root.appendChild( child7 );  
		        root.appendChild( child8 ); 
		        
		        
		        // write the content into xml file
		        try{
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				StreamResult result = new StreamResult(new File("C:\\file.xml"));
		 
				// Output to console for testing
				//StreamResult result = new StreamResult(System.out);
		 
				transformer.transform(source, result);
		 
				System.out.println("File saved!");
		 
			  } catch (TransformerException tfe) {
				tfe.printStackTrace();
			  }
		        textField_2.setText("");
				textField_3.setText("");
				textField_4.setText("");
				textField_5.setText("");
				textField_6.setText("");
				textField_7.setText("");
				textField_8.setText("");
				textField_9.setText("");
	
			}		
			
		});
		btnDodajpromeni.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_btnDodajpromeni = new GridBagConstraints();
		gbc_btnDodajpromeni.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDodajpromeni.insets = new Insets(0, 0, 5, 5);
		gbc_btnDodajpromeni.gridx = 2;
		gbc_btnDodajpromeni.gridy = 5;
		add(btnDodajpromeni, gbc_btnDodajpromeni);
		
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.HORIZONTAL;
		gbc_scrollPane.anchor = GridBagConstraints.WEST;
		gbc_scrollPane.insets = new Insets(5, 0, 0, 0);
		gbc_scrollPane.ipady = 200;
		gbc_scrollPane.gridwidth = 10;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 7;
		add(scrollPane, gbc_scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
    }
}